package top.yvyan.guettable.Gson;

import static java.lang.Integer.parseInt;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import top.yvyan.guettable.bean.CourseBean;

public class ClassTableNew {
    public List<studentTableVms> studentTableVms;

    public static class studentTableVms {
        public static class lessonSearch {
            public static class Course {
                public String code; // 课程代码
            }

            public Course course;

            public String code; // 课号

        }
        public List<Lesson> activities;

        public List<lessonSearch> lessonSearchVms;

        public Map<String,String> getCourseCodeToLessonCodeMap() {
            Map<String,String> map = new HashMap<>();

            if (activities != null && !activities.isEmpty()) {
                for (Lesson lesson : activities) {
                    if(lesson.lessonId >= 0) {
                        map.put(lesson.courseCode, lesson.lessonCode);
                    }
                }
            }

            if (lessonSearchVms != null && !lessonSearchVms.isEmpty()) {
                for (lessonSearch lesson : lessonSearchVms) {
                    map.put(lesson.course.code,lesson.code);
                }
            }

            return map;
        } // for lab
    }

    public static class Lesson {
        private CourseType courseType;

        public static class CourseType {
            public String name;
        }

        private Integer lessonId;
        private String lessonCode;
        private String courseName;
        private String courseCode;
        private String projectName;
        private String room;
        private String roomDescribe;
        private List<String> teachers;
        private String startTime;
        private String endTime;
        private int[] weekIndexes;
        private Integer startUnit;
        private Integer endUnit;
        private Double credits;
        private Integer weekday;
        private String lessonRemark;

        public String getCourseTime() {
            if (lessonId < 0) {
                return null;
            }
            if (startTime != null && !startTime.isEmpty() &&
                    endTime != null && !endTime.isEmpty()) {
                return startTime + "~" + endTime;
            }
            return null;
        }

        public String getTeacher() {
            return String.join(" ", teachers);
        }

        public List<CourseBean> toCourseBean(Map<String,String> courseCodeToLessonCodeMap) {
            if (lessonRemark == null) {
                lessonRemark = "";
            }
            List<CourseBean> courseBeans = new ArrayList<>();
            String formatedRoom = null;
            Arrays.sort(weekIndexes);
            if (room == null) {
                room = "";
            }
            if (lessonId < 0) {
                formatedRoom = (roomDescribe != null && !roomDescribe.isEmpty() ? roomDescribe + " " + room : room);
                for (int week : weekIndexes) {
                    CourseBean courseBean = new CourseBean();
                    if (courseCodeToLessonCodeMap!=null && courseCodeToLessonCodeMap.containsKey(lessonCode)) {
                        lessonCode = courseCodeToLessonCodeMap.get(lessonCode);
                    }
                    courseBean.setLab(lessonCode, courseName, projectName, formatedRoom, week, weekday, startUnit, endUnit, this.getTeacher(), lessonRemark);
                    courseBeans.add(courseBean);
                }
                return courseBeans;
            }
            if (!room.isEmpty()) {
                formatedRoom = (roomDescribe != null && !roomDescribe.isEmpty() ? room + " " + roomDescribe : room);
            }

            int lastWeekIndex = weekIndexes[0];
            int startWeek = lastWeekIndex;
            for (int week : weekIndexes) {
                if (week - lastWeekIndex > 1) {
                    CourseBean courseBean = new CourseBean();
                    courseBean.setCourse(lessonCode, courseName, formatedRoom, startWeek, lastWeekIndex, weekday, startUnit, endUnit, this.getCourseTime(), this.getTeacher(), lessonRemark);
                    courseBeans.add(courseBean);
                    startWeek = week;
                }
                lastWeekIndex = week;
            }
            CourseBean courseBean = new CourseBean();
            courseBean.setCourse(lessonCode, courseName, formatedRoom, startWeek, lastWeekIndex, weekday, startUnit, endUnit, this.getCourseTime(), this.getTeacher(), lessonRemark);
            courseBeans.add(courseBean);
            return courseBeans;
        }

    }

}
